<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * New_model short summary.
 *
 * New_model description.
 *
 * @version 1.0
 * @author xianbin
 */
class New_model extends CI_Model
{
    public $new_id;
    public $title;
    public $contents;
    public $imgurl;
    public $menuid;
    public $weight;
    public $adddate;
    public $keyword;
    public $description;

    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }
    public function add_new($newModel)
    {
        return  $this->db->insert('new',$newModel);
    }

    public function get_first_row_by_menuid($menuid)
    {
        $sql="select * from new where menuid=$menuid";
        return $this->db->query($sql)->row();
    }

    public function get_new_by_id($new_id)
    {
        $sql="select * from new where new_id=$new_id";
        return $this->db->query($sql)->row();
    }

    public function update_new($newModel)
    {
        $sql="update new set title='$newModel->title',contents='$newModel->contents',imgurl='$newModel->imgurl',weight='$newModel->weight',adddate='$newModel->adddate',keyword='$newModel->keyword',description='$newModel->description' where new_id=$newModel->new_id";
        return $this->db->query($sql);
    }

    public function del_new($new_id)
    {
        $sql="delete from new where new_id=$new_id";
        return $this->db->query($sql);
    }

    public function batch_del_new($nidArray)
    {
        $this->db->trans_begin();
        foreach($nidArray as $niditem)
        {
            $this->db->delete('new',array('new_id'=>$niditem));
        }
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            return 0;
        }
        else
        {
            $this->db->trans_commit();
            return 1;
        }
    }

    public function get_page($cpage,$pagesize,$files,$tabel,$order,$temp_id,$where,$temStr)
    {
        $sql="SELECT $files FROM $tabel a JOIN (select $temp_id from $tabel $where $order limit ".($cpage-1)*$pagesize.", $pagesize) b ON a.$temp_id = b.$temp_id $temStr $order";
        $sql_count="select count('$temp_id') as _count from $tabel $where";
        $data['objlist']=$this->db->query($sql)->result();
        $data['count']=$this->db->query($sql_count)->result();
        return $data;
    }

    public function set_new_weight($newModel)
    {
        return   $this->db->query("update new set weight=".$newModel->weight." where new_id=".$newModel->new_id);
    }
}